<?php
class goldgoodsControl extends skymvc{
	public $userid;
	public function __construct(){
		parent::__construct();
	}
	
	public function onInit(){
		if(in_array(get('a'),array("my","save","add","mylove"))){
			M("login")->checklogin();
		}
		$this->userid=M("login")->userid;
		if($this->userid){
			$row=M("goldgoods_user")->selectRow("userid=".$this->userid);
			if(empty($row)){
				M("goldgoods_user")->insert(array("userid"=>$this->userid));
			}
		}
	}
	
	function onDefault(){
		$rscount=true;
		$where=" status=2  ";
		$url="/index.php?m=goldgoods";
		
		$start=get_post('per_page','i');
		$limit=20;
		$option=array(
			"where"=>$where,
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"last_time DESC"
		);
		$data=M("goldgoods")->select($option,$rscount);
		if($data){
			$t_ids=array();
			foreach($data as $k=>$v){
				$t_ids[]=$v['catid'];
				$t_uids[]=$v['userid'];
			}
			if($t_ids){
				$t_c=M("category")->cat_list(" catid in("._implode($t_ids).")");
			}
			
			if($t_uids){
				$t_u=M("user")->getUserByIds($t_uids);
			}
			foreach($data as $k=>$v){
				$v['cname']=$t_c[$v['catid']];
				if(isset($t_u[$v['userid']])){
					$v['nickname']=$t_u[$v['userid']]['nickname'];
				}
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$sdata=array(
			"list"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist
		);
		 
		$this->smarty->goAssign($sdata);
		$this->smarty->display("goldgoods/index.html");
		 
	}
	
	 
	 
	public function onAddClick(){
		$id=get_post('id','i');
		$row=M("goldgoods")->selectRow("id=".$id);
		if($row){
			M("goldgoods")->update(array("view_num"=>$row['view_num']+1),"id=".$id);
		}
		 
	 }
	
}
?>